package com.bobo.service.message;

import com.bobo.dao.message.IMessageDao;
import com.bobo.dao.message.MessageDaoImpl;
import com.bobo.dto.Page;
import com.bobo.model.Message;

import java.util.List;

/**
 * Created by bobo on 16-10-29.
 */
public class MessageServiceImpl implements IMessageService {

    IMessageDao messageDao = new MessageDaoImpl();

    @Override
    public Integer create(Message message) {
        return messageDao.create(message);
    }

    @Override
    public boolean del(Integer id) {
        return messageDao.delete(id);
    }

    @Override
    public Boolean upd(Message message) {
        return messageDao.upd(message);
    }

    @Override
    public Message getById(Integer id) {
        return messageDao.getById(id);
    }

    @Override
    public Page<Message> getMessagesByTopic(Integer topicId, Integer current, Integer pageSize, Integer status) {
        Page<Message> page = new Page<>();
        page.setCurrent(current);
        page.setPageSize(pageSize);
        Integer count = messageDao.getCount(null,topicId,status);
        float pagenum = count.floatValue()/pageSize.floatValue();
        page.setPageNum((int) Math.ceil(pagenum));
        page.setList( messageDao.getMessagesByTopic(topicId,current,pageSize,status));
        return page;
    }

    @Override
    public Page<Message> getMessagesByUser(Integer userId, Integer current, Integer pageSize, Integer status) {
        Page<Message> page = new Page<>();
        page.setCurrent(current);
        page.setPageSize(pageSize);
        Integer count = messageDao.getCount(userId,null,status);
        float pagenum = count.floatValue()/pageSize.floatValue();
        page.setPageNum((int) Math.ceil(pagenum));
        page.setList(messageDao.getMessagesByUser(userId,current,pageSize,status));
        return page;
    }

    @Override
    public Page<Message> getMessages(Integer current, Integer pageSize, Integer status) {
        Page<Message> page = new Page<>();
        page.setCurrent(current);
        page.setPageSize(pageSize);
        Integer count = messageDao.getCount(null,null,status);
        float pagenum = count.floatValue()/pageSize.floatValue();
        page.setPageNum((int) Math.ceil(pagenum));
        page.setList(messageDao.getMessages(current,pageSize,status));
        return page;
    }
}
